package com.lkx.iocproject;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.lkx.iocproject.inject.ContentView;
import com.lkx.iocproject.inject.OnClick;
import com.lkx.iocproject.inject.OnLongClick;
import com.lkx.iocproject.inject.ViewInject;
import com.lkx.iocproject.proxy.HttpProxy;
import com.lkx.iocproject.proxy.MyInvocationHandler;
import com.lkx.iocproject.proxy.OkhttpRequest;

import java.lang.reflect.Proxy;

@ContentView(R.layout.activity_main)
public class MainActivity extends BaseActivity {

    @ViewInject(R.id.btnOne)
    Button btnOne;
    @ViewInject(R.id.btnTwo)
    Button btnTwo;
    @ViewInject(R.id.btnThree)
    Button btnThree;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        btnOne.setText("btnOne");
        btnThree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("lkx_debug","btnThree onclick");
            }
        });
        btnThree.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Log.d("lkx_debug","btnThree onLongClick");
                return true;
            }
        });
    }

    public void testProxy(View view) {
        OkhttpRequest okhttpRequest = new OkhttpRequest();
        HttpProxy httpProxy = (HttpProxy) getProxyObj(okhttpRequest);
        httpProxy.get("开始执行");
    }

    /**
     * 获取代理类对象
     * @return
     */
    public Object getProxyObj(Object obj){
        Object instance = Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), new MyInvocationHandler(obj));
        return instance;
    }

    @OnClick(ids = {R.id.btnOne, R.id.btnTwo})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btnOne:
                Toast.makeText(getApplicationContext(),"点击了btnOne",Toast.LENGTH_SHORT).show();
                break;
            case R.id.btnTwo:
                Toast.makeText(getApplicationContext(),"点击了BtnTwo",Toast.LENGTH_SHORT).show();
                break;
        }
        Log.d("lkx_debug","onclick");
    }

    @OnLongClick(ids = {R.id.btnOne, R.id.btnTwo})
    public boolean onLongClick(View view){
        Log.d("lkx_debug","onLongClick");
        switch (view.getId()){
            case R.id.btnOne:
                Toast.makeText(getApplicationContext(),"长按了btnOne",Toast.LENGTH_SHORT).show();
                break;
            case R.id.btnTwo:
                Toast.makeText(getApplicationContext(),"长按了BtnTwo",Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }

}